package com.ruoyi.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.domain.ZhwyChargeitemRentalcontract;
import com.ruoyi.domain.ZhwyChargeitemRentalcontractVo;
import com.ruoyi.mapper.ZhwyDefaultchargesMapper;
import com.ruoyi.service.ZhwyChargeItemContractService;
import com.ruoyi.system.domain.ZhwyShops;
import com.ruoyi.system.service.IZhwyShopsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.ZhwyRentalcontract;
import com.ruoyi.service.IZhwyRentalcontractService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 租售合同Controller
 * 
 * @author ruoyi
 * @date 2024-05-07
 */
@RestController
@RequestMapping("/hyj/rentalcontract")
public class ZhwyRentalcontractController extends BaseController
{
    @Autowired
    private IZhwyRentalcontractService zhwyRentalcontractService;

    @Autowired
    private ZhwyChargeItemContractService midService;
    @Autowired
    private ZhwyDefaultchargesMapper zhwyDefaultchargesMapper;
    @Autowired
    private IZhwyShopsService shopsService;
    /**
     * 查询租售合同列表
     */
    @PreAuthorize("@ss.hasPermi('hyj:rentalcontract:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZhwyRentalcontract zhwyRentalcontract)
    {
        startPage();
        List<ZhwyRentalcontract> list = zhwyRentalcontractService.selectZhwyRentalcontractList(zhwyRentalcontract);
        return getDataTable(list);
    }
    @GetMapping("/listshop")
    public TableDataInfo listshop(ZhwyRentalcontract zhwyRentalcontract)
    {
        List<ZhwyShops> shops=new ArrayList<>();
        // startPage();
        List<ZhwyRentalcontract> list = zhwyRentalcontractService.selectZhwyRentalcontractList(zhwyRentalcontract);
        for (ZhwyRentalcontract item : list) {
            ZhwyShops zhwyShops = shopsService.selectZhwyShopsById(item.getShopsId());
            shops.add(zhwyShops);
        }
        return getDataTable(shops);
    }

    /**
     * 导出租售合同列表
     */
    @PreAuthorize("@ss.hasPermi('hyj:rentalcontract:export')")
    @Log(title = "租售合同", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZhwyRentalcontract zhwyRentalcontract)
    {
        List<ZhwyRentalcontract> list = zhwyRentalcontractService.selectZhwyRentalcontractList(zhwyRentalcontract);
        ExcelUtil<ZhwyRentalcontract> util = new ExcelUtil<ZhwyRentalcontract>(ZhwyRentalcontract.class);
        util.exportExcel(response, list, "租售合同数据");
    }

    /**
     * 获取租售合同详细信息
     */
    @PreAuthorize("@ss.hasPermi('hyj:rentalcontract:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(zhwyRentalcontractService.selectZhwyRentalcontractById(id));
    }

    /**
     * 新增租售合同
     */
    @PreAuthorize("@ss.hasPermi('hyj:rentalcontract:add')")
    @Log(title = "租售合同", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZhwyRentalcontract zhwyRentalcontract)
    {
        zhwyRentalcontract.setType(1);
        shopsService.changeStateById(zhwyRentalcontract.getShopsId(),1);
        zhwyRentalcontractService.insertZhwyRentalcontract(zhwyRentalcontract);
        Long cID = zhwyRentalcontractService.findByReMarkAndTrade(zhwyRentalcontract.getTrade(), zhwyRentalcontract.getRemark());
        // System.out.println(cID);
        List<Long> longs = zhwyDefaultchargesMapper.selectAll();
        // System.out.println(longs);
        for (Long id : longs) {
        ZhwyChargeitemRentalcontract have = new ZhwyChargeitemRentalcontract();
            have.setChargeItemId(id);
            have.setRentalContractId(cID);
             midService.saveItemContract(have);
        }
        return toAjax(true);
    }
    @PostMapping("/vo")
    public AjaxResult add(@RequestBody ZhwyChargeitemRentalcontractVo zhwyRentalcontract)
    {
        ZhwyRentalcontract rentalcontract = zhwyRentalcontractService.selectZhwyRentalcontractById(zhwyRentalcontract.getRentalContractId());
        Long contractId = rentalcontract.getId();

        Long[] chargeItemIds = zhwyRentalcontract.getChargeItemId();
        if(zhwyRentalcontract.getChargeItemId().length==0||
                zhwyRentalcontract.getChargeItemId()==null){
            return toAjax(true);
        }
        for (Long chargeItemId : chargeItemIds) {
            ZhwyChargeitemRentalcontract have = new ZhwyChargeitemRentalcontract();
            have.setChargeItemId(chargeItemId);
            have.setRentalContractId(contractId);
            midService.saveItemContract(have);
        }
        return toAjax(true);
    }

    /**
     * 修改租售合同
     */
    @PreAuthorize("@ss.hasPermi('hyj:rentalcontract:edit')")
    @Log(title = "租售合同", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZhwyRentalcontract zhwyRentalcontract)
    {
        return toAjax(zhwyRentalcontractService.updateZhwyRentalcontract(zhwyRentalcontract));
    }

    /**
     * 删除租售合同
     */
    @PreAuthorize("@ss.hasPermi('hyj:rentalcontract:remove')")
    @Log(title = "租售合同", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        for (Long id : ids) {

            ZhwyRentalcontract rentalcontract = zhwyRentalcontractService.selectZhwyRentalcontractById(id);
            Long shopsId = rentalcontract.getShopsId();
            shopsService.changeStateById(shopsId,0);
        }
        return toAjax(zhwyRentalcontractService.deleteZhwyRentalcontractByIds(ids));
    }
    /* 撤销合同 */
    @GetMapping(value = "/rollback/{id}")
    // @Anonymous
    public AjaxResult rollback(@PathVariable("id") Long id)
    {
        return success(zhwyRentalcontractService.rollbackById(id));
    }
}
